#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include <QToolBar>
#include <QMenuBar>
#include <QAction>
#include <QStatusBar>
#include <QDebug>
#include <QLabel>
#include <QProgressBar>
#include <QPushButton>
#include <QDockWidget>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QColor>
#include <QColorDialog>
#include <cstdio>
#include <sstream>
#include <QFileDialog>
#include <QFont>
#include <QFontDialog>
#include <QInputDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /////////////创建QMenuBar//////////////
    ///QMenuBar -> QMenu -> QAction
    /// 程序会自动帮我们生成一个menubar，设置新的menubar会导致其从对象树上脱离，
    /// 造成内存泄漏，在多窗口上容易出现问题，单窗口相对没问题。

    /*
    //创建menubar的规范写法
    //如果存在的话，自己获取自己；否则就创建一个
    QMenuBar* menubar = this->menuBar();
    this->setMenuBar(menubar);

    //设置快捷键
    QMenu* menu1 = new QMenu("文件(&F)");
    QMenu* menu2 = new QMenu("编辑(&E)");
    QMenu* menu3 = new QMenu("视图(&V)");

    menubar->addMenu(menu1);
    menubar->addMenu(menu2);
    menubar->addMenu(menu3);

    QMenu* menu4 = new QMenu("保存");
    QAction* action2 = new QAction("关闭");
    QAction* action3 = new QAction("新建");
    QAction* action4 = new QAction("打开");

    //点击触发triggered信号
    menu1->addAction(action3);
    menu1->addAction(action4);
    //添加分隔符
    menu1->addSeparator();
    //menu1->addAction(action1);
    menu1->addAction(action2);
    menu1->addMenu(menu4);

    connect(action2, &QAction::triggered, this, &QMainWindow::close);

    //添加图标
    QPixmap pixmap(":/dabian.svg");
    QIcon icon(pixmap);
    action2->setIcon(icon);

    //////////////创建QToolBar////////////////

    QToolBar* toolbar = new QToolBar(this);
    this->addToolBar(toolbar); //添加菜单是setMenuBar，添加工具栏是addToolBar
    QAction* action_1 = new QAction("关闭");
    QAction* action_2 = new QAction("保存");
    toolbar->addAction(action_1);
    toolbar->addAction(action_2);

    connect(action_1, &QAction::triggered, this, &QMainWindow::close);

    /////////////设置ToolTip///////////////

    action_1->setToolTip("点击以关闭程序");
    action_2->setToolTip("点击以保存文件");
    */

    //////////////QToolBar////////////////

    /*
    QToolBar* toolbar = new QToolBar();
    QToolBar* toolbar2 = new QToolBar();

    this->addToolBar(toolbar);
    this->addToolBar(Qt::LeftToolBarArea, toolbar2);

    toolbar2->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);

    QAction* action1 = new QAction("动作一");
    QAction* action2 = new QAction("动作二");

    toolbar->addAction(action1);
    toolbar2->addAction(action2);
    */

    //////////////QStatusBar///////////////

    /*
    QStatusBar* statusbar = this->statusBar();
    this->setStatusBar(statusbar);
    QLabel* label = new QLabel("Downloading...");
    statusbar->addWidget(label, 1); //添加widget并设置占比
    QProgressBar* progressbar = new QProgressBar();
    progressbar->setRange(0, 100);
    progressbar->setValue(50);
    statusbar->addWidget(progressbar, 2);
    QPushButton* button = new QPushButton("退出安装");
    statusbar->addWidget(button);
    connect(button, &QPushButton::clicked, this, &MainWindow::close);
    */

    /////////////QDockWidget//////////////

    /*
    QDockWidget* dockwidget = new QDockWidget();
    this->addDockWidget(Qt::LeftDockWidgetArea, dockwidget);
    dockwidget->setWindowTitle("这是一个dockwidget");
    dockwidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);

    //想要在dockWidget中添加部件需要先添加一个QWidget，再添加一个组件
    QWidget* container = new QWidget();
    dockwidget->setWidget(container); //新建一个QLabel之后，需要将其设置进QDockWidget
    QVBoxLayout* layout = new QVBoxLayout();
    container->setLayout(layout);
    QLabel* label = new QLabel("这是一个QLabel");
    QPushButton* pushbutton = new QPushButton("这是一个QPushButton");
    layout->addWidget(label);
    layout->addWidget(pushbutton);
    */

    //////////////QDialog///////////////

    /*
    QPushButton* button = new QPushButton(this);
    connect(button, &QPushButton::clicked, this, [this](){
        dialog* dialog_1 = new dialog(this);
        dialog_1->resize(400, 200);
        dialog_1->setWindowTitle("this is a QDialog");
        //delete dialog; //采取这种写法对话框就会一闪而过
        dialog_1->setAttribute(Qt::WA_DeleteOnClose); //Qt内置的方法，当点击对话框时释放QDialog
        dialog_1->show(); //显示对话框
    });
    */

    ///////////model and bool: 模态与非模态////////////

    //将对话框的show改为delete，即可以让用户必须在当前对话框做出交互
    /*
    dialog* dialog_1 = new dialog(this);
    dialog_1->setWindowTitle("Warning");
    dialog_1->exec();
    */

    /////////////QMessageBox//////////////

    //Icon: Warning, NoIcon, Information, Critical, Question
    //StandardButton: Ok, Save, Cancel
    /*
    QPushButton* button_2 = new QPushButton(this);
    connect(button_2, &QPushButton::clicked, this, [this]()
    {
    QMessageBox* messagebox = new QMessageBox(this);
    messagebox->setWindowTitle("this is a window title");
    messagebox->setIcon(QMessageBox::Warning);
    messagebox->setStandardButtons(QMessageBox::Ok | QMessageBox::Save | QMessageBox::Cancel);
    //messagebox->exec();
    //获取那些按钮被点击
    int result = messagebox->exec();
    if(result == QMessageBox::Ok) //即可以作为按键，也可以作为接受返回值
        {
        qDebug() << "Ok";
    }
    else if(result == QMessageBox::Save)
        {
        qDebug() << "Save";
    }
    else if(result == QMessageBox::Cancel)
        {
        qDebug() << "Cancel";
    }
    messagebox->setAttribute(Qt::WA_DeleteOnClose);
    */

    //buttonRole
    /*
    QPushButton* button = new QPushButton(this);
    messagebox->addButton(button, QMessageBox::AcceptRole);
    QPushButton* button1 = new QPushButton(this);
    messagebox->addButton(button1, QMessageBox::RejectRole);
    });
    */

    //通过一个warning函数直接构建出整个对话框
    //对橡树挂载指针、窗口标题、窗口文本、按钮
    //informaton, quesion等均可
    /*
    int result = QMessageBox::warning(this, "Warning!", "Don't close the window!", QMessageBox::Ok | QMessageBox::Close);
    if(result == QMessageBox::Ok)
    {
        qDebug() << "Ok";
    }
    else if(result == QMessageBox::Close)
    {
        qDebug() << "Close";
    }
    */

    //////////////QColorDialog////////////////

    //供用户通过调色板选择颜色
    //方法一：通过Qt的方式拼接QStyleSheet
    /*
    QColor color = QColorDialog::getColor(QColor(0, 255, 0), this, "Choose a color for the mainWindow");
    QString style = "background-color: rgb(" + QString::number(color.red()) + QString::number(color.green()) + QString::number(color.blue()) + ")";
    this->setStyleSheet(style);
    */

    //方法二：通过C语言的方式
    /*
    QColor color = QColorDialog::getColor(QColor(0, 255, 0), this, "Choose a color for the mainWindow");
    char style[1024];
    sprintf(style, "background-color: rgb(%d, %d, %d)", color.red(), color.green(), color.blue());
    this->setStyleSheet(style);
    */

    //方法三：通过C++的方式
    /*
    QColor color = QColorDialog::getColor(QColor(0, 255, 0), this, "choose a color");
    std::stringstream style;
    style << "background-color: rgb(" << color.red() << ", " << color.green() << ", " << color.blue() << ")";
    std::string stylee;
    style >> stylee;
    this->setStyleSheet(QString::fromStdString(stylee));
    */

    /////////////QFileDialog/////////////

    //通过QFileDialog可以获取一个文件路径，可以打开或者保存文件。
    //getOpenFileName, getSaveFileName
    //不需要创建对象，直接使用静态成员函数就可以

    /*
    QPushButton* button_save = new QPushButton("Save", this);
    QPushButton* button_open = new QPushButton("Open", this);
    button_open->move(200, 200);
    QLabel* label = new QLabel(this);
    label->move(400, 400);
    connect(button_save, &QPushButton::clicked, this, [this, label](){
        label->setText(QFileDialog::getSaveFileName());
    });
    connect(button_open, &QPushButton::clicked, this, [this, label](){
        label->setText(QFileDialog::getOpenFileName());
    });
    */

    //////////////QFontDialog//////////////

    //也和QFileDialog一样，不需要创建对象，直接使用静态成员函数就可以
    /*QPushButton* button = new QPushButton("按钮button", this);
    connect(button, &QPushButton::clicked, this, [button](){
        bool ok = false;
        QFont font = QFontDialog::getFont(&ok);
        qDebug() << font;
        button->setFont(font);
    });*/

    /////////////QInputDialog/////////////
    /// getInt getDouble getItem（通过QStringList来实现）
    ///

    QPushButton* button1 = new QPushButton(this);
    connect(button1, &QPushButton::clicked, this, [this](){
        QStringList item;
        item.push_back("111");
        item.push_back("222");
        item.push_back("333");
        item.push_back("444");
        QString result = QInputDialog::getItem(this, "请输入或选择条目", "请选择条目", item);
        qDebug() << result;
    });

}
MainWindow::~MainWindow()
{
    delete ui;
}
